package cn.wolfcode.web.controller;


import cn.wolfcode.common.domain.UserInfo;
import cn.wolfcode.common.web.Result;
import cn.wolfcode.common.web.anno.RequireLogin;
import cn.wolfcode.common.web.resolver.RequestUser;
import cn.wolfcode.domain.OrderInfo;
import cn.wolfcode.domain.PayResult;
import cn.wolfcode.service.IOrderInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/orderPay")
@RefreshScope
@Slf4j
public class OrderPayController {

    @Autowired
    private IOrderInfoService orderInfoService;

    /**
     * 发起支付
     */
    @RequireLogin
    @GetMapping("/pay")
    public Result<String> pay(String orderNo, Integer type, @RequestUser UserInfo userInfo) {
        // 判断类型调用不同api
        if (type.equals(OrderInfo.PAY_TYPE_ONLINE)) {
            return Result.success(orderInfoService.onlinePay(orderNo));
        }
        orderInfoService.integralPay(orderNo, userInfo.getPhone());
        return Result.success("");
    }

    /**
     * 支付成功
     */
    @PostMapping("/success")
    public Result<?> alipaySuccess(@RequestBody PayResult payResult) {
        orderInfoService.alipaySuccess(payResult);
        return Result.success("");
    }

    /**
     * 退款
     */
    @GetMapping("/refund")
    public Result<String> refund(String orderNo) {
        orderInfoService.refund(orderNo);
        return Result.success("退款成功");
    }
}
